package com.cci.kangdao.task;

import com.cci.kangdao.crmapi.CRMGeocodeUtil;
import com.cci.kangdao.dao.ServiceStationDao;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class GaoDeTask {

    @Autowired
    private ServiceStationDao serviceStationDao;


	//查看服务站表中哪些服务站没有经纬度，通过该方法调用高德接口把地址转化为经纬度
    public void workOrderExceptionSynchronization(){
        List<Map<String,Object>> serviceStationList  = serviceStationDao.getNullPositionLatPositionLonServiceStationList();
        if(CollectionUtils.isNotEmpty(serviceStationList)){
            for(Map<String,Object> serviceStation : serviceStationList){
                Map<String, Object> geoMap = new HashedMap<String, Object>();
                geoMap.put("ADDRESS", serviceStation.get("Address").toString());
                CRMGeocodeUtil.geocode(geoMap);// 根据地址信息转化出经纬度
                if(StringUtils.isEmpty(geoMap.get("lat").toString())){
                    String address = serviceStation.get("Province").toString()+serviceStation.get("City").toString()+serviceStation.get("Area").toString()+serviceStation.get("Address").toString();
                    geoMap.put("ADDRESS", address);
                    CRMGeocodeUtil.geocode(geoMap);// 根据地址信息转化出经纬度
                }
                System.out.println(geoMap.get("lat").toString()  +  "    " + geoMap.get("lon").toString());
                serviceStation.put("PositionLat", geoMap.get("lat"));
                serviceStation.put("PositionLon", geoMap.get("lon"));
                Map<String,Object> map = new HashMap<>();
                map.put("province",serviceStation.get("Province"));
                map.put("city",serviceStation.get("City"));
                map.put("area",serviceStation.get("Area"));
                map.put("address",serviceStation.get("Address"));
                map.put("telephone",serviceStation.get("Telephone"));
                map.put("phone",serviceStation.get("Phone"));
                map.put("lat",geoMap.get("lat"));
                map.put("lon",geoMap.get("lon"));
                map.put("serviceStationId",serviceStation.get("ID"));
                serviceStationDao.updateServiceStation(map);
            }
        }
    }


    //获取一个服务站有多个站长的excel数据，除了指定的服务站站长，其余的变为服务技师
    public void updateServiceStationMangerData(){
        XSSFWorkbook wb = null;
        try {
            // 获取excel
            wb = new XSSFWorkbook(new File("C:\\Users\\ljk\\Desktop\\正式环境-一家服务商存在多个站长数据\\正式环境-一家服务商存在多个站长数据-XCEC.xlsx"));
            Sheet sh = wb.getSheetAt(3);
            for (int i = 1; i <= sh.getLastRowNum(); i++) {
                Row row = sh.getRow(i);
                String stationNo = row.getCell(0).getStringCellValue();
                String stationName = row.getCell(1).getStringCellValue();
                String userName = row.getCell(2).toString();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

	 
}
